package com.example.download

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.download.databinding.ActivityHomeBinding
import com.example.download.fragment.AFragment
import com.example.download.fragment.CFragment
import com.example.download.fragment.BFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class Home : AppCompatActivity() {
    private lateinit var activityHomeBinding: ActivityHomeBinding
    private lateinit var tablayout: TabLayout
    private lateinit var homeViewPager2 :ViewPager2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        activityHomeBinding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(activityHomeBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        tablayout = activityHomeBinding.homeTablayout1
        homeViewPager2 = activityHomeBinding.homeViewpager1
        homeViewPager2.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount(): Int {
                return 3
            }

            override fun createFragment(position: Int): Fragment {
                return when(position){
                    0 -> AFragment()
                    1 -> BFragment()
                    2 -> CFragment()
                    else -> AFragment()
                }
            }
        }
        TabLayoutMediator(tablayout, homeViewPager2) { tab, position ->
            when (position) {
                0 -> {
                    tab.text = "Home"
                    tab.setIcon(R.mipmap.smile_home)
                }
                1 -> {
                    tab.text = "Downloading"
                    tab.setIcon(R.mipmap.download_file)
                }
                2 -> {
                    tab.text = "Downloaded"
                    tab.setIcon(R.mipmap.folder_download_file)
                }
            }
        }.attach()
        }

    }
